package com.rs.sm;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;  Java版本不支持
import java.util.Base64;

//public class Base64Example {
//    public static void main(String[] args) {
//        String originalString = "Hello, World!";
//        String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
//        System.out.println("Encoded String: " + encodedString);
//
//        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
//        String decodedString = new String(decodedBytes);
//        System.out.println("Decoded String: " + decodedString);
//    }
//}

public class SM4Utils {
    public String secretKey = "";
    private String iv = "UISwD9fW6cFh9SNS";
    public boolean hexString = false;

    public SM4Utils() {
    }

    public String encryptData_ECB(String plainText) {
        try {
            SM4_Context ctx = new SM4_Context();
            ctx.isPadding = true;
            ctx.mode = 1;
            byte[] keyBytes;
            if (this.hexString) {
                keyBytes = Util.hexStringToBytes(this.secretKey);
            } else {
                keyBytes = this.secretKey.getBytes();
            }

            SM4 sm4 = new SM4();
            sm4.sm4_setkey_enc(ctx, keyBytes);
            byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("utf-8"));
//            String cipherText = (new BASE64Encoder()).encode(encrypted);
            String cipherText = Base64.getEncoder().encodeToString(encrypted);
            if (cipherText != null && cipherText.trim().length() > 0) {
                Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                Matcher m = p.matcher(cipherText);
                cipherText = m.replaceAll("");
            }

            return cipherText;
        } catch (Exception var9) {
            Exception e = var9;
            e.printStackTrace();
            return null;
        }
    }


//    public byte[] encryptData_ECB(File file) throws IOException {
//        FileInputStream inputFile = new FileInputStream(file);
//        byte[] buffer = new byte[(int)file.length()];
//        inputFile.read(buffer);
//        inputFile.close();
//        return (new BASE64Decoder()).decodeBuffer(this.encryptData_ECB((new BASE64Encoder()).encode(buffer)));
//    }

    public String decryptData_ECB(String cipherText) {
        try {
            SM4_Context ctx = new SM4_Context();
            ctx.isPadding = true;
            ctx.mode = 0;
            byte[] keyBytes;
            if (this.hexString) {
                keyBytes = Util.hexStringToBytes(this.secretKey);
            } else {
                keyBytes = this.secretKey.getBytes();
            }

            SM4 sm4 = new SM4();
            sm4.sm4_setkey_dec(ctx, keyBytes);
            
//            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, (new BASE64Decoder()).decodeBuffer(cipherText));
            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Base64.getDecoder().decode(cipherText));
            return new String(decrypted, "utf-8");
        } catch (Exception var6) {
            Exception e = var6;
            e.printStackTrace();
            return null;
        }
    }

//
//    public byte[] decryptData_ECB(File file) throws IOException {
//        FileInputStream inputFile = new FileInputStream(file);
//        byte[] buffer = new byte[(int)file.length()];
//        inputFile.read(buffer);
//        inputFile.close();
//        return (new BASE64Decoder()).decodeBuffer(this.decryptData_ECB((new BASE64Encoder()).encode(buffer)));
//    }

//    public String encryptData_CBC(String plainText) {
//        try {
//            SM4_Context ctx = new SM4_Context();
//            ctx.isPadding = true;
//            ctx.mode = 1;
//            byte[] keyBytes;
//            byte[] ivBytes;
//            if (this.hexString) {
//                keyBytes = Util.hexStringToBytes(this.secretKey);
//                ivBytes = Util.hexStringToBytes(this.iv);
//            } else {
//                keyBytes = this.secretKey.getBytes();
//                ivBytes = this.iv.getBytes();
//            }
//
//            SM4 sm4 = new SM4();
//            sm4.sm4_setkey_enc(ctx, keyBytes);
//            byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("utf-8"));
//            String cipherText = (new BASE64Encoder()).encode(encrypted);
//            if (cipherText != null && cipherText.trim().length() > 0) {
//                Pattern p = Pattern.compile("\\s*|\t|\r|\n");
//                Matcher m = p.matcher(cipherText);
//                cipherText = m.replaceAll("");
//            }
//
//            return cipherText;
//        } catch (Exception var10) {
//            Exception e = var10;
//            e.printStackTrace();
//            return null;
//        }
//    }

//    public byte[] encryptData_CBC(File file) throws IOException {
//        FileInputStream inputFile = new FileInputStream(file);
//        byte[] buffer = new byte[(int)file.length()];
//        inputFile.read(buffer);
//        inputFile.close();
//        return (new BASE64Decoder()).decodeBuffer(this.encryptData_CBC((new BASE64Encoder()).encode(buffer)));
//    }

//    public String decryptData_CBC(String cipherText) {
//        try {
//            SM4_Context ctx = new SM4_Context();
//            ctx.isPadding = true;
//            ctx.mode = 0;
//            byte[] keyBytes;
//            byte[] ivBytes;
//            if (this.hexString) {
//                keyBytes = Util.hexStringToBytes(this.secretKey);
//                ivBytes = Util.hexStringToBytes(this.iv);
//            } else {
//                keyBytes = this.secretKey.getBytes();
//                ivBytes = this.iv.getBytes();
//            }
//
//            SM4 sm4 = new SM4();
//            sm4.sm4_setkey_dec(ctx, keyBytes);
//            byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, (new BASE64Decoder()).decodeBuffer(cipherText));
//            return new String(decrypted, "utf-8");
//        } catch (Exception var7) {
//            Exception e = var7;
//            e.printStackTrace();
//            return null;
//        }
//    }

//    public byte[] decryptData_CBC(File file) throws IOException {
//        FileInputStream inputFile = new FileInputStream(file);
//        byte[] buffer = new byte[(int)file.length()];
//        inputFile.read(buffer);
//        inputFile.close();
//        return (new BASE64Decoder()).decodeBuffer(this.decryptData_CBC((new BASE64Encoder()).encode(buffer)));
//    }
}
